package com.fadadianqi.mall.admin.controller;

import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.mall.product.model.ItemSPU;
import com.fadadianqi.mall.product.service.AttrService;
import com.fadadianqi.mall.product.service.ItemSPUService;
import com.fadadianqi.mall.service.NavService;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by aroot on 2/28/14.
 */
@Controller
public class ItemSKUController {

    @Autowired
    private AttrService attrService;
    @Autowired
    private NavService navService;
    @Autowired
    private ItemSPUService itemSPUService;
    @Autowired
    private GoodsService goodsService;


    @RequestMapping(value = "/mall/adm/product/inventory/items")
    public String showItems(HttpServletRequest request, ModelMap modelMap) {
        List<ItemSPUController.ItemVO> items = new ArrayList<ItemSPUController.ItemVO>();
        Pagination pagination = new Pagination(request, 30);
        List<ItemSPU> spuList = itemSPUService.findAll(pagination);
        if (ValidatorHelper.isNotEmpty(spuList)) {
            for (ItemSPU spu : spuList) {
                ItemSPUController.ItemVO vo = new ItemSPUController.ItemVO();
                BeanUtils.copyProperties(vo, spu);
                vo.setBrand(goodsService.getBrandById(spu.getBrandId()));
                //List<Nav> navList = navService.findBySpuId(vo.getId());
                vo.setNavList(navService.findBySpuId(vo.getId()));
                vo.setAttrValList(attrService.findAttrValBySpuId(spu.getId()));
                items.add(vo);
            }
        }
        modelMap.put("items", items);
        return "/mall/admin/inventory/items";
    }
}
